#include <iostream>
#include <map>
using namespace std;

int n,m,sum = 0;
string id,oldAlumniBirth = "99999999",oldAlumniId,oldBirth = "99999999",oldId;
map<string,int> alumni;

signed main()
{
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>id;
        alumni[id] = 1;
    }
    cin>>m;
    for(int i=0;i<m;i++){
        cin>>id;
        string t = id.substr(6,8);
        if(alumni.find(id)!=alumni.end()){
            sum++;
            if(t<oldAlumniBirth){
                oldAlumniBirth = t;
                oldAlumniId = id;
            }
        }
        if(t<oldBirth){
            oldBirth = t;
            oldId = id;
        }
    }
    cout<<sum<<"\n";
    if(sum)    cout<<oldAlumniId<<"\n";
    else    cout<<oldId<<"\n";
    return 0;
}
